package com.core.shared;

import com.core.model.lxf.Good;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-12-11
 * Time: 下午5:08
 * To change this template use File | Settings | File Templates.
 */
public class IndexWriterDemo {

    public static void main(String[] args) {
//        createIndex();
        createIndexByLxf();
    }

    public static void createIndex() {
        IndexWriter writer = null;
        try {
            Directory directory = new RAMDirectory();  //索引文件存存放位置：内存中
//            Directory directory = FSDirectory.open(Paths.get("D:/itxxz/lucene"));     //索引文件存存放位置：目录下
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(directory, iwc);
            Document document;
            File f = new File("D:/itxxz/data");
            for (File file : f.listFiles()) {
                System.out.println("filename:" + file.getName());
                document = new Document();
                document.add(new LongField("modified", f.lastModified(), Field.Store.NO));
                document.add(new TextField("contents", new FileReader(file)));
                document.add(new StringField("path", file.toString(), Field.Store.YES));
                writer.addDocument(document);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void createIndexByLxf() {
        IndexWriter writer = null;
        try {
            Directory directory = new RAMDirectory();  //索引文件存存放位置：内存中
//            Directory directory = FSDirectory.open(Paths.get("D:/itxxz/lucene"));     //索引文件存存放位置：目录下
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
            writer = new IndexWriter(directory, iwc);
            Document document;
            List<Good> list = new ArrayList<>();
            Good g1 = new Good("刘仙飞", "21");
            Good g2 = new Good("离开的减飞肥", "22");
            Good g3 = new Good("立刻仙决定是否", "23");
            Good g4 = new Good("了斯达康刘减肥", "24");
            list.add(g1);
            list.add(g2);
            list.add(g3);
            list.add(g4);
            for (Good good : list) {
                document = new Document();
                document.add(new TextField("guid", good.guid(), Field.Store.YES));   //YES NO ,是否允许取值
                document.add(new TextField("name", good.name(), Field.Store.YES));
                document.add(new TextField("age", good.age(), Field.Store.YES));
                writer.addDocument(document);
            }
            writer.commit();
            //*********************************
            Query query;
            IndexReader reader = DirectoryReader.open(directory);
            IndexSearcher searcher = new IndexSearcher(reader);
            QueryParser queryParser = new QueryParser("name", new StandardAnalyzer());
            query = queryParser.parse("啥");
            if (searcher != null) {
                TopDocs topDocs = searcher.search(query, reader.maxDoc());
                if (topDocs.totalHits > 0) {
                    for (ScoreDoc sr : topDocs.scoreDocs) {
                        Document doc = searcher.doc(sr.doc);
                        System.out.println("guid= " + doc.get("guid"));
                        System.out.println("name= " + doc.get("name"));
                        System.out.println("age= " + doc.get("age"));
                    }
                } else {
                    System.out.println("无数据");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void testQuery() throws IOException, ParseException {
        Query query;
        Directory directory = new RAMDirectory();
        IndexReader reader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(reader);
        QueryParser queryParser = new QueryParser("name", new StandardAnalyzer());
        query = queryParser.parse("刘仙飞");
        if (searcher != null) {
            TopDocs topDocs = searcher.search(query, 1);
            if (topDocs.totalHits > 0) {
                System.out.println("找到: " + topDocs.totalHits + "  个结果! ");
            }
        }
    }

}
